<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\service\Cashier as CashierService;
use think\response\Json;

/**
 * 订单付款控制器 (收银台)
 * Class Cashier
 * @package app\api\controller
 */
class Cashier extends Controller {
	/**
	 * 获取支付订单的信息
	 * @param int $orderId 订单ID
	 * @param string $client 指定的客户端
	 * @return Json
	 * @throws \cores\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function orderInfo(int $orderId, string $client): Json {
		$CashierService = new CashierService;
		$data = $CashierService->setOrderId($orderId)->setClient($client)->orderInfo();
		return $this->renderSuccess($data);
	}

	/**
	 * 确认订单支付事件
	 * @param int $orderId 订单ID
	 * @param string $method 支付方式
	 * @param string $client 指定的客户端
	 * @param array $extra 附加数据
	 * @return Json
	 * @throws \cores\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function orderPay(int $orderId, string $method, string $client, array $extra = []): Json {
		$CashierService = new CashierService;
		$data = $CashierService->setOrderId($orderId)
			->setMethod($method)
			->setClient($client)
			->orderPay($extra);
		return $this->renderSuccess($data, $CashierService->getMessage() ?: '下单成功');
	}

	/**
	 * 确认订单支付事件-native
	 * @param int $orderId 订单ID
	 * @param string $method 支付方式
	 * @param string $client 指定的客户端
	 * @param array $extra 附加数据
	 * @return Json
	 * @throws \cores\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function nativePay(int $orderId, string $method, string $client, array $extra = []): Json {
		$CashierService = new CashierService;
		$data = $CashierService->setOrderId($orderId)
			->setMethod($method)
			->setClient($client)
			->nativePay($extra);

		// 获取二维码路径
		$savePath = $this->getFilePath(10001, 'native',$orderId);
		$url = $data['payment']['code_url'];

		// 生成二维码
		\PHPQRCode\QRcode::png($url, $savePath, 'L', 15, 1);
		$data['qrcode_url'] = $this->getPosterUrl(10001,$orderId);
		return $this->renderSuccess($data, $CashierService->getMessage() ?: '下单成功');
	}

     /**
     * 二维码url
     * @return string
     */
    private function getPosterUrl($storeId,$orderId): string
    {
            // 文件名称
        $fileName = 'native_' . $orderId . '.png';
        return base_url() . 'temp/image/' . $storeId . '/' . $fileName . '?t=' . time();
    }

	/**
	 * 获取二维码路径
	 * @param int $storeId
	 * @param string $scene
	 * @param string|null $page
	 * @param string $channel 二维码渠道(小程序码、h5码)
	 * @return string
	 */
	private function getFilePath(int $storeId, string $scene, int $orderId): string {
		// 文件目录
		$dirPath = web_path()  . "temp/image/{$storeId}";
		!is_dir($dirPath) && mkdir($dirPath, 0755, true);
		// 文件名称
		$fileName = 'native_' . $orderId . '.png';
		// 文件路径
		return "{$dirPath}/{$fileName}";
	}

	/**
	 * 交易查询
	 * @param string $outTradeNo 商户订单号
	 * @param string $method 支付方式
	 * @param string $client 指定的客户端
	 * @return Json
	 * @throws \cores\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function tradeQuery(string $outTradeNo, string $method, string $client): Json {
		$CashierService = new CashierService;
		$result = $CashierService->setMethod($method)->setClient($client)->tradeQuery($outTradeNo);
		$message = $result ? '订单已付款成功' : ($CashierService->getError() ?: '很抱歉，订单未支付，请重新发起');
		return $this->renderSuccess(['isPay' => $result], $message);
	}
}